package a01_数组;

/**
 * <p>
 * a04_有序数组的平方复习5
 * </p>
 *
 * @author flyduck
 * @since 2024/12/25
 */
public class a04_有序数组的平方复习5 {
    public int[] sortedSquares(int[] nums) {
        int[] newArray = new int[nums.length];
        int newIdx = newArray.length - 1;

        int leftIdx = 0;
        int rightIdx = nums.length -1;
        while (leftIdx <= rightIdx){
            if((nums[leftIdx] * nums[leftIdx]) >= nums[rightIdx] * nums[rightIdx]){
                newArray[newIdx] = nums[leftIdx] * nums[leftIdx];
                leftIdx++;
                newIdx--;
            }else {
                newArray[newIdx] = nums[rightIdx] * nums[rightIdx];
                rightIdx--;
                newIdx--;
            }
        }
        return newArray;
    }
}
